package com.mcxx.util;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;


public class RSAUtils {
    public static String publicKey="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCxBKMpxKFnchXZXBfzYVudSwa4clTI5zUE986MyM5nYaPx4zXiBX4V99kVs9SN0NCxgqvx/fbRVXe2A6HF4zG4+6kn+9iVwy9prOXwn1lE+1AjeL+ywMZRB3zx17ApARAYsHEFaF3LtinreEcoTLtc5LdweF8VjZkpMkT2Sf/qeQIDAQAB";
    public static String privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCfemESeaSQKGiXG9aUeSSpemM8\n" +
            "/4jelh4+stWq5jL/Qfl8+uUSC7KJAPhRLM+rXlxseB/V2l0yv3PO04+a1loKYIQjHivpMZ3/xHrW\n" +
            "UsxCdDqUjHWnh8CFTCzTGbcMosV8LaKSpUgofnazppwDjrkWxuRm4cKWBTRj9A/9gL45YKN6iSZz\n" +
            "fOwvtf5cQbuK5dMZ7o9HKlXjuERCGYrwmDjUzc9+VcojTdoAgiOC9GpY8iXIhZrdpuPkVOcGQ7HG\n" +
            "s0WX7E/V37RGiWbT8yupL/2maQ6RxbFPd7fr78egJKrsdBadNGHozYYcYiIYBJ5Q4I7jXmL4T9Q9\n" +
            "fcnmCwGMrpIdAgMBAAECggEBAITKeA3UMAzBOOWqDnCutrJP8FYYSLVetmxqqLOW3pvhKYbBuv/d\n" +
            "sZmWry2YbIs7FSZUl92P586b0ChFzOuW1H6TgzNiqhmQcQlJZycS5nXnJegPVOBcjxfvJ9vXK65f\n" +
            "0qTlstr8uSrOsrRAZOQpcUDf98sa7NXSG5+m17G0A7Dt0F1BQgQCSzuqV8x6/OpbohX7X8vc7gXQ\n" +
            "/U67w+TQX8Sp5WHUDg0FM/vek6eBzNVeymKs10DJkzbGDBi11+5mFFxadP/HvHdyE4REyC5c9xNH\n" +
            "ECVfTxJC+xiSOvsqSqDpfzzaPqvaoq9E+1gdXyD4sAsVbCU1asAkmK94MqfVyQECgYEAzsJb4D/s\n" +
            "dxHgCRiKxA8sbaQvq9w+Z/Cqa93UVzERzbwNrW1/Nj64N8ASoht02y/EqN6kqF5PBhsS7j6iG/JF\n" +
            "sgPxqLwoKrIO67JCFdcSmaCL5wxHjre7q0iQNqOL1LBoZBog1DN9nt5MtRcV83byIo8ksAlONssp\n" +
            "ZiXkAf2nWWECgYEAxXVk9mNum/NfcQiq1CSH06iXduAwemkXAbQxv9gWSTifz3H8liiN73ui25tR\n" +
            "8K+VPXs8M4T/OJQU218mCMN7uL1ofJZllKLn9dCZDf/zudVnJQrd4YI60Y8cQkCHgimGLzZi/zdz\n" +
            "K1wIta8uAJ++BhSHzVLPNnUjV/58Mx12Bj0CgYA3Rd4p5OziJ/UseVMfdq754Jqd5xMH1l6412Sc\n" +
            "qpGH3m4JnuwTJFE7Fi3lCFIFBpAcVOjP2eZmS3aUGkpcDjhzjnpcEXDreQ5kFBN2ozhluXWtHqtC\n" +
            "i/uvnoZvFOmcwkwl9pYwJFPA1MzM3I8RtwqkEoBMTzCJlWZL2HVL0vOaAQKBgG/oBl41H/y2Q0L2\n" +
            "XIp6YZgtFhpDGHZnlKhcjU1ik87PjpKs0S/wr/Anq7a2Igw8FmKPsyCnoVVZZzCkjhOtZjLe9ZaW\n" +
            "Td+dK8T+gZ+TjXemYBx4OiOSAO+/IY7Sr3mvOuMK+bQWoujR8gW10GZ+Id2kSQqugc0lOfCmw5Dc\n" +
            "g4UpAoGBAM2xanLJeA5J0Sfw2ixifTJyjr3SlL2UvLhJndF8nz0tE+oXxQ1xdLj9Y8HbhFzwqkYO\n" +
            "eKUn4TdM/kwXSsIMCFOuY9uzc+vMVvJTi/whH9vaKitU+sGJ9soMRFbSziWMxdiK092vNE4LzZQX\n" +
            "Z7umtapbGKMT/2/fo832wnhObepd";


    public static final String ALGORITHM="RSA";


    private static final int KEY_SIZE=2048;


    public static KeyPair generateKeyPair() throws Exception {

        KeyPairGenerator gen = KeyPairGenerator.getInstance(ALGORITHM);


        gen.initialize(KEY_SIZE);


        return gen.generateKeyPair();
    }


    public static PublicKey getPublicKey(String pubKeyBase64) throws Exception {

        byte[] encPubKey = new BASE64Decoder().decodeBuffer(pubKeyBase64);


        X509EncodedKeySpec encPubKeySpec = new X509EncodedKeySpec(encPubKey);


        return KeyFactory.getInstance(ALGORITHM).generatePublic(encPubKeySpec);
    }


    public static PrivateKey getPrivateKey(String priKeyBase64) throws Exception {

        byte[] encPriKey = new BASE64Decoder().decodeBuffer(priKeyBase64);


        PKCS8EncodedKeySpec encPriKeySpec = new PKCS8EncodedKeySpec(encPriKey);


        return KeyFactory.getInstance(ALGORITHM).generatePrivate(encPriKeySpec);
    }


    public static byte[] encrypt(byte[] plainData, PublicKey pubKey) throws Exception {

        Cipher cipher = Cipher.getInstance(ALGORITHM);


        cipher.init(Cipher.ENCRYPT_MODE, pubKey);


        return cipher.doFinal(plainData);
    }

    private static byte[] clientEncrypt(String publicKey, byte[] plainData) throws Exception {

        PublicKey pubKey=RSAUtils.getPublicKey(publicKey);


        byte[] cipher = RSAUtils.encrypt(plainData, pubKey);
        return cipher;
    }


    private static String encryptStr(String publicKey, String dataStr) throws Exception {

        PublicKey pubKey=RSAUtils.getPublicKey(publicKey);


        byte[] cipher = RSAUtils.encrypt(dataStr.getBytes(), pubKey);

        return Base64.getEncoder().encodeToString(cipher);
    }


    public static byte[] decrypt(byte[] cipherData, PrivateKey priKey) throws Exception {

        Cipher cipher = Cipher.getInstance(ALGORITHM);


        cipher.init(Cipher.DECRYPT_MODE, priKey);


        return cipher.doFinal(cipherData);
    }


    public byte[] webEncrypt(String publicKey, String data) throws Exception {
        byte[] cipherData=clientEncrypt(publicKey, data.getBytes());
        return cipherData;
    }


    public static String decryptStr(String privateKey, String cipherStr) throws Exception {
        byte[] crypt = Base64.getDecoder().decode(cipherStr);

        PrivateKey priKey = RSAUtils.getPrivateKey(privateKey);

        byte[] plainData = RSAUtils.decrypt(crypt, priKey);

        return new String(plainData);
    }

    public static void main(String[] args) throws Exception {


        String c = "iUliyC5vmH2tvf8xKF/oIamaI/JccTG+qFrmv9FImZYJmiy2+J9wcxBoNhDOTwOJDLXa8jKAXiVu05DB2ufM3Lys/tnS8mxtHj8CK6RXOsus4Y0TCli2E4HUAGPEPLyPzEFB9+JuGLuffZf+dv+GZ+nR19tM0WfmYyreDcSJqUCBpSG0uEzOUYzdMwgTTvapuCDnERFTAZ50az43yEcr/ivwVTU6cuRK74hhEFro62kvSABu6yddb9opbMZi2WuHFjblYngW56Gf4rVT8MHyJwoJgU3+N7p86X1CqGH8OEFLQT9Toe3hiHO6mUPZJ+42zGxURu8Cy9AqYfTczBs8JA==";
        String str1 = RSAUtils.decryptStr(RSAUtils.privateKey, c);
        System.out.println(str1);


    }
}













